package org.gap.service;

import java.util.List;
import java.util.Set;

import org.gap.dao.PatientDAO;
import org.gap.dao.RendezVousDAO;

import org.gap.domain.Patient;
import org.gap.domain.RendezVous;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for RendezVous entities
 * 
 */

@Service("RendezVousService")
@Transactional
public class RendezVousServiceImpl implements RendezVousService {

	/**
	 * DAO injected by Spring that manages Patient entities
	 * 
	 */
	@Autowired
	private PatientDAO patientDAO;

	/**
	 * DAO injected by Spring that manages RendezVous entities
	 * 
	 */
	@Autowired
	private RendezVousDAO rendezVousDAO;

	/**
	 * Instantiates a new RendezVousServiceImpl.
	 *
	 */
	public RendezVousServiceImpl() {
	}

	/**
	 * Load an existing RendezVous entity
	 * 
	 */
	@Transactional
	public Set<RendezVous> loadRendezVouss() {
		return rendezVousDAO.findAllRendezVouss();
	}

	/**
	 * Save an existing RendezVous entity
	 * 
	 */
	@Transactional
	public void saveRendezVous(RendezVous rendezvous) {
		RendezVous existingRendezVous = rendezVousDAO.findRendezVousByPrimaryKey(rendezvous.getRdvRendezVous());

		if (existingRendezVous != null) {
			if (existingRendezVous != rendezvous) {
				existingRendezVous.setRdvRendezVous(rendezvous.getRdvRendezVous());
				existingRendezVous.setRdvTiers(rendezvous.getRdvTiers());
				existingRendezVous.setRdvSpecialite(rendezvous.getRdvSpecialite());
				existingRendezVous.setRdvDate(rendezvous.getRdvDate());
				existingRendezVous.setRdvAdmission(rendezvous.getRdvAdmission());
			}
			rendezvous = rendezVousDAO.store(existingRendezVous);
		} else {
			rendezvous = rendezVousDAO.store(rendezvous);
		}
		rendezVousDAO.flush();
	}

	/**
	 * Return all RendezVous entity
	 * 
	 */
	@Transactional
	public List<RendezVous> findAllRendezVouss(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<RendezVous>(rendezVousDAO.findAllRendezVouss(startResult, maxRows));
	}

	/**
	 * Delete an existing Patient entity
	 * 
	 */
	@Transactional
	public RendezVous deleteRendezVousPatient(Integer rendezvous_rdvRendezVous, Integer related_patient_patPatient) {
		RendezVous rendezvous = rendezVousDAO.findRendezVousByPrimaryKey(rendezvous_rdvRendezVous, -1, -1);
		Patient related_patient = patientDAO.findPatientByPrimaryKey(related_patient_patPatient, -1, -1);

		rendezvous.setPatient(null);
		related_patient.getRendezVouses().remove(rendezvous);
		rendezvous = rendezVousDAO.store(rendezvous);
		rendezVousDAO.flush();

		related_patient = patientDAO.store(related_patient);
		patientDAO.flush();

		patientDAO.remove(related_patient);
		patientDAO.flush();

		return rendezvous;
	}

	/**
	 * Delete an existing RendezVous entity
	 * 
	 */
	@Transactional
	public void deleteRendezVous(RendezVous rendezvous) {
		rendezVousDAO.remove(rendezvous);
		rendezVousDAO.flush();
	}

	/**
	 * Save an existing Patient entity
	 * 
	 */
	@Transactional
	public RendezVous saveRendezVousPatient(Integer rdvRendezVous, Patient related_patient) {
		RendezVous rendezvous = rendezVousDAO.findRendezVousByPrimaryKey(rdvRendezVous, -1, -1);
		Patient existingpatient = patientDAO.findPatientByPrimaryKey(related_patient.getPatPatient());

		// copy into the existing record to preserve existing relationships
		if (existingpatient != null) {
			existingpatient.setPatPatient(related_patient.getPatPatient());
			existingpatient.setPatDateEntree(related_patient.getPatDateEntree());
			existingpatient.setPatTelContact(related_patient.getPatTelContact());
			existingpatient.setPatDivers(related_patient.getPatDivers());
			existingpatient.setPatDateSortie(related_patient.getPatDateSortie());
			existingpatient.setPatAxeMedical(related_patient.getPatAxeMedical());
			existingpatient.setPatSousAxeMedical(related_patient.getPatSousAxeMedical());
			existingpatient.setPatMotifSortie(related_patient.getPatMotifSortie());
			existingpatient.setPatDiagnostic(related_patient.getPatDiagnostic());
			existingpatient.setPatDateDerniereVisite(related_patient.getPatDateDerniereVisite());
			related_patient = existingpatient;
		} else {
			related_patient = patientDAO.store(related_patient);
			patientDAO.flush();
		}

		rendezvous.setPatient(related_patient);
		related_patient.getRendezVouses().add(rendezvous);
		rendezvous = rendezVousDAO.store(rendezvous);
		rendezVousDAO.flush();

		related_patient = patientDAO.store(related_patient);
		patientDAO.flush();

		return rendezvous;
	}

	/**
	 */
	@Transactional
	public RendezVous findRendezVousByPrimaryKey(Integer rdvRendezVous) {
		return rendezVousDAO.findRendezVousByPrimaryKey(rdvRendezVous);
	}

	/**
	 * Return a count of all RendezVous entity
	 * 
	 */
	@Transactional
	public Integer countRendezVouss() {
		return ((Long) rendezVousDAO.createQuerySingleResult("select count(o) from RendezVous o").getSingleResult()).intValue();
	}
}
